<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 出差管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="query.name" style="width: 150px" placeholder="用户名"  class="handle-input mr10"></el-input>
                <el-date-picker v-model="query.month" type="month" placeholder="选择月"></el-date-picker>&nbsp;
                <el-button type="primary" icon="el-icon-search" @click="getData">搜索</el-button>
                <el-button type="primary" class="handle-del mr10" @click="addErrands()">
                    添加出差信息
                </el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="errandId" label="出差编号" align="center"></el-table-column>
                <el-table-column prop="empId" label="员工编号" align="center"></el-table-column>
                <el-table-column prop="name" label="员工名" align="center"></el-table-column>
                <el-table-column label="出差时间" prop="errandTime" align="center"></el-table-column>
                <el-table-column prop="errandDay" label="出差天数" align="center">
                </el-table-column>
                <el-table-column prop="errandPlace" label="出差地点" align="center"></el-table-column>
                <el-table-column prop="errandSubsidy" label="每日补贴" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">

                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">

                <el-form-item label="出差时间">
                    <el-date-picker v-model="form.errandTime" type="date" ></el-date-picker>
                </el-form-item>
                <el-form-item label="出差天数">
                    <el-input v-model="form.errandDay"></el-input>
                </el-form-item>
                <el-form-item label="出差地点">
                    <el-input v-model="form.errandPlace"></el-input>
                </el-form-item>
                <el-form-item label="每日补贴">
                    <el-input v-model="form.errandSubsidy"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {batchDelPost, deleteErrand, errandData,  selectAllList, updateErrand,getById} from '../../api/errand';
    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    name: '',
                    pageIndex: 1,
                    pageSize: 3,
                    month:null
                },
                place:[],

                listAll:[],
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getErrand();
            //this.getErrandName();
        },
        methods: {
            getErrand()
            {
                selectAllList(this.query).then((res) => {
                    const data = res;
                    this.tableData = data.data.data;
                    this.pageTotal = data.data.count;
                });
            },
            addErrands() {
                this.$router.push('/addErrand');
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                if(this.query.month!=null)
                {
                    let month = this.query.month.toString();
                    let d = new Date(month);

                    let monthData = d.getFullYear()+"-"+(d.getMonth()+1)+"-1";
                    this.query.month = monthData;
                }
                console.log(this.query.month)
                selectAllList(this.query).then((res) => {
                    const data = res;
                    this.tableData = data.data.data;
                    this.pageTotal = data.data.count;
                    console.log(this.pageTotal);
                });
            },

            /*     // 触发搜索按钮
                 handleSearch() {
                     this.$set(this.query, 'pageIndex', 1);
                     this.getData();
                 },*/
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除该员工的出差信息吗？', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        const $this = this;
                        deleteErrand(row.errandId).then((res) => {
                            $this.$message.success('删除成功');
                            $this.getData();
                            $this.idx = index;
                        });
                    })
                    .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                getById(row.errandId).then((res) => {
                    this.form = res.data;

                    console.log(this.form.errandTime)
                });
                this.editVisible = true;
                this.getData();
            },

            //批量删除
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    const errandId = this.multipleSelection[i].errandId;
                    str += errandId + ',';
                }
                const errandIds = str.substring(0, str.lastIndexOf(','));

                if(errandIds=="")
                {
                    this.$message.warning("请选择你需要的删除的员工出差信息");
                }
                else {
                    // 二次确认删除
                    this.$confirm(`确定要删除编号为(${errandIds})员工出差信息吗？`, '警告', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    })
                        .then(() => {
                            //删除最后一个逗号
                            const $this = this;
                            batchDelPost(errandIds).then((res) => {
                                $this.$message.error(`删除了${errandIds}`);
                                $this.multipleSelection = [];
                                $this.getErrand();
                            });
                        })
                        .catch(() => {});
                }
            },

            // 保存编辑
            // 提交按钮
            submitForm: function () {
                const $this = this;
                this.$refs['form'].validate((valid) => {
                    if (valid) {

                        updateErrand(this.form).then((response) => {
                            $this.$message.success('修改成功');
                            this.getData()
                            this.editVisible = false;
                            //$this.cancel();
                        });
                    }
                });
            },
            reset() {
                this.form = {
                    errandTime: undefined,
                    errandDay: undefined,
                    errandPlace: undefined,
                    errandSubsidy: undefined,
                };
            },
            cancel() {
                this.open = false;
                this.reset();
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getErrand();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>